package com.kekezu.kppw.data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class CaseData {
	static FinalDb db;

	/**
	 * 用户个人案例信息
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getPersonCase(
			Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/personCase?token=" + users.get(0).getToken();
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);

			JSONObject jsonObject = new JSONObject(strJson);

			String resString = jsonObject.getString("code");

			// Log.e("rescode", resString);
			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				JSONArray jsonArray = jsondata.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("title", itemJsonObject.getString("title"));
					map.put("pic", itemJsonObject.getString("pic"));

					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}

	/**
	 * 案例详情
	 * 
	 * @param caseId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getCaseInfo(String caseId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/caseInfo?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", caseId);

		if (users.size() > 0) {
			params.put("token", "" + users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));// 任务id
				resMap.put("uid", jsondata.getString("uid"));// 任务收藏状态
				resMap.put("desc", jsondata.getString("desc"));// 标题
				resMap.put("url", jsondata.getString("url"));// 详情
				resMap.put("title", jsondata.getString("title"));// 浏览量
				resMap.put("pic", jsondata.getString("pic"));// 投稿数
				resMap.put("cate_id", jsondata.getString("cate_id"));// 金额
				resMap.put("type", jsondata.getString("type"));// 任务开始时间
				resMap.put("pub_uid", jsondata.getString("pub_uid"));// 任务开始时间
				resMap.put("view_count", jsondata.getString("view_count"));// 任务状态
				resMap.put("created_at", jsondata.getString("created_at"));// 电话
			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 添加个人案例信息
	 * 
	 * @param strTitle
	 * @param strCateId
	 * @param strDesc
	 * @param strPic
	 * @param context
	 */
	public static String[] addCase(String strTitle, String strCateId,
			String strDesc, String strPic, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context) + "user/addCase";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		try {
			File file1 = new File(strPic);

			MultipartEntity multipart = new MultipartEntity();
			multipart.addPart("title",
					new StringBody(strTitle, Charset.forName("UTF-8")));
			multipart.addPart("cate_id", new StringBody(strCateId));
			multipart.addPart("pic", new FileBody(file1));
			multipart.addPart("desc",
					new StringBody(strDesc, Charset.forName("UTF-8")));
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

}
